package com.zzstm.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzstm.commonUtils.JwtUtils;
import com.zzstm.commonUtils.MD5;
import com.zzstm.educenter.entity.UcenterMember;
import com.zzstm.educenter.entity.vo.RegisterVO;
import com.zzstm.educenter.mapper.UcenterMemberMapper;
import com.zzstm.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzstm.servicebase.ex.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-11-12
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //登录
    @Override
    public String login(UcenterMember member) {
        String mobile = member.getMobile();
        String pwd = member.getPassword();
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",member.getMobile());
        UcenterMember mobileMember = baseMapper.selectOne(queryWrapper);
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(pwd)){
            throw new GuliException(20001,"登录失败");
        }
        if (!mobile.equals(mobileMember.getMobile())){
            throw new GuliException(20001,"登录失败");
        }
        if (!MD5.encrypt(pwd).equals(mobileMember.getPassword())){
            throw new GuliException(20001,"登录失败");
        }
        if (mobileMember.getIsDisabled()){
            throw new GuliException(20001,"登录失败");
        }
        //登录成功，生成token
        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(),mobileMember.getNickname());
        return jwtToken;
    }

    //注册
    @Override
    public void register(RegisterVO registerVO) {
        String nickname = registerVO.getNickname();
        String mobile = registerVO.getMobile();
        String password = registerVO.getPassword();
        String code = registerVO.getCode();
//        if (StringUtils.isEmpty(nickname) || StringUtils.isEmpty(mobile)
//            || StringUtils.isEmpty(password) || StringUtils.isEmpty(code)){
//            return false;
////            throw new GuliException(20001,"数据不能为空");
//        }
//        String redisCode = redisTemplate.opsForValue().get(mobile);
//        if (redisCode == null){
//            return false;
////            throw new GuliException(20001,"验证码已过期");
//        }
//        if (!redisCode.equals(code)){
//            return false;
////            throw new GuliException(20001,"验证码错误");
//        }
//        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
//        wrapper.eq("mobile",mobile);
//        Integer count = baseMapper.selectCount(wrapper);
//        if (count > 0){
//            return false;
////            throw new GuliException(20001,"用户已存在");
//        }
        UcenterMember user = new UcenterMember();
        user.setAvatar("https://zzstm.oss-cn-hangzhou.aliyuncs.com/avatar/634d20c0866142f4adaba14cb944dc4efile.png");
        user.setIsDisabled(false);
        user.setMobile(mobile);
        user.setNickname(nickname);
        user.setPassword(MD5.encrypt(password));
        baseMapper.insert(user);
    }

    //查询某天注册人数
    @Override
    public Integer countRegister(String day) {
        Integer count = baseMapper.countRegister(day);
        return count;
    }
}
